﻿using System.Collections.Generic;
using System.Windows;
using GameEngine.Clients.Silverlight;
using GameEngine.Clients.Silverlight.View;
using GameEngine.Model.Data;
using MySettlers.View.Components;
using PureMVC.Interfaces;
using PureMVC.Patterns;
using GameEngine.View.Components;
using GameEngine.View;
using MySettlers.Game.View.Components;
using GameEngine.Model;
using MySettlers.Things;

namespace MySettlers.View
{
    public class LevelMenuMediator : MenuMediator<string,LevelMenuUC>
    {
        ILevelDataProxy<int> dataProxy;

        public const string MYNAME = "LevelMenuMediator";
        public LevelMenuMediator(LevelMenuUC view, string outCommand, ILevelDataProxy<int> dp)
            : base(MYNAME, view, outCommand) 
        {
            dataProxy = dp;
        }

        public new LevelMenuUC ViewComponent
        {
            get { return base.ViewComponent as LevelMenuUC; }
        }

        #region IMediator Members

        /// <summary>
        /// List the <c>INotification</c> names this <c>Mediator</c> is interested in being notified of
        /// </summary>
        /// <returns>The list of <c>INotification</c> names </returns>
        override public IList<string> ListNotificationInterests()
        {
            IList<string> notifications = new List<string>(base.ListNotificationInterests());
            var world = MyModel.Helper.GetWorldDataProxy(MyFacade.Instance.currentWorldId);
            notifications.Add(world.THINGADDED_NOTIFICATION);
            notifications.Add(world.THINGREMOVED_NOTIFICATION);
            return notifications;
        }

        /// <summary>
        /// Handle <c>INotification</c>s
        /// </summary>
        /// <param name="notification">The <c>INotification</c> instance to handle</param>
        /// <remarks>
        ///     <para>
        ///        Typically this will be handled in a switch statement, with one 'case' entry per <c>INotification</c> the <c>Mediator</c> is interested in. 
        ///     </para>
        /// </remarks>
        override public void HandleNotification(INotification notification)
        {
            base.HandleNotification(notification);
            foreach (var world in dataProxy.Worlds.Values)
            {
                if (notification.Name.Equals(world.THINGADDED_NOTIFICATION)) {
                    ViewComponent.setNumberOfPeasants(world.Items.Count);
                
                }
                else if (notification.Name.Equals(world.THINGREMOVED_NOTIFICATION))
                {
                    ViewComponent.setNumberOfPeasants(world.Items.Count);
                }
            }
        }

        /// <summary>
        /// Called by the View when the Mediator is registered
        /// </summary>
        override public void OnRegister()
        {
            base.OnRegister();
            ViewComponent.OnExternalActionButtonClick += new OnButtonClickHandler<string>(ViewComponent_OnButtonClick);
            //ViewComponent.setNumberOfFood();
            foreach (var world in dataProxy.Worlds)
            {
                int count = 0;
                foreach(var item in world.Value.Items.Values)
                    if(item is PeasantDataProxy)
                        count ++;
                ViewComponent.setNumberOfPeasants(count);
                break;
            }
        }

        /// <summary>
        /// Called by the View when the Mediator is removed
        /// </summary>
        override public void OnRemove()
        {
            base.OnRemove();
        }

        #endregion

        void ViewComponent_OnButtonClick(string text)
        {
            switch (text)
            {
                case LevelMenuUC.BUILDBUTTON:
                    SendNotification(MyFacade.PAGE_GAME_NEWTHING_IN_COMMAND);
                    break;
            }
        }
    }
}